package com.doublethread.easymart.modules.user.controller;

import com.doublethread.easymart.common.result.Result;
import com.doublethread.easymart.modules.user.dto.LoginDTO;
import com.doublethread.easymart.modules.user.dto.RegisterDTO;
import com.doublethread.easymart.modules.user.service.UserService;
import com.doublethread.easymart.modules.user.vo.LoginVO;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户认证控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/auth")
public class AuthController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<LoginVO> login(@Valid @RequestBody LoginDTO loginDTO) {
        log.info("用户登录");

        LoginVO loginVO = userService.login(loginDTO);

        log.info("用户登录成功");

        return Result.success(loginVO);
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result<Void> register(@Valid @RequestBody RegisterDTO registerDTO) {
        log.info("用户注册");

        userService.register(registerDTO);

        log.info("用户注册成功");
        return Result.success();
    }

    /**
     * 用户登出
     */
    @PostMapping("/logout")
    public Result<Void> logout() {
        log.info("用户登出");
        return Result.success();
    }

}
